/**
 * FileResourceHyperlink.java
 * created by wesley at 2008-6-7
 */
package net.sf.filelink.hyperlink;

import java.util.List;

import net.sf.filelink.utl.QueryUtils;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.internal.text.html.HTMLPrinter;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.hyperlink.IHyperlink;

/**
 * FileResourceHyperlink
 * 
 * @author wesley
 */
public class FileResourceHyperlink implements IHyperlink {

    private final IRegion m_region;

    private final String m_nodeInfo;

    /**
     * constructor for FileResourceHyperlink
     * 
     * @param region
     * @param filePath
     */
    public FileResourceHyperlink(IRegion region, String nodeInfo) {
        m_region = region;
        m_nodeInfo = nodeInfo;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.text.hyperlink.IHyperlink#getHyperlinkRegion()
     */
    public IRegion getHyperlinkRegion() {
        return m_region;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.text.hyperlink.IHyperlink#getHyperlinkText()
     */
    public String getHyperlinkText() {

        StringBuffer infoBuff = new StringBuffer();
        HTMLPrinter.addPageProlog(infoBuff);
        HTMLPrinter.addParagraph(infoBuff, "Files:");

        HTMLPrinter.startBulletList(infoBuff);

        // search file by path
        String filePath = QueryUtils.toFilePath(m_nodeInfo);
        List<IFile> fileList = QueryUtils.findFileResources(filePath, QueryUtils.getSearchFolders());
        for (IFile file : fileList) {
            String fileInfo = file.getProjectRelativePath().toString();
            HTMLPrinter.addBullet(infoBuff, HTMLPrinter.convertToHTMLContent(fileInfo));
        }

        // search file by name
        String fileName = QueryUtils.toFileName(m_nodeInfo);
        if (!fileName.equals(filePath)) {
            fileList = QueryUtils.findFileResources(fileName, QueryUtils.getSearchFolders());
            for (IFile file : fileList) {
                String fileInfo = file.getProjectRelativePath().toString();
                HTMLPrinter.addBullet(infoBuff, HTMLPrinter.convertToHTMLContent(fileInfo));
            }
        }
        HTMLPrinter.endBulletList(infoBuff);

        HTMLPrinter.addPageEpilog(infoBuff);

        return infoBuff.toString();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.text.hyperlink.IHyperlink#getTypeLabel()
     */
    public String getTypeLabel() {
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.text.hyperlink.IHyperlink#open()
     */
    public void open() {

        // search file by path
        String filePath = QueryUtils.toFilePath(m_nodeInfo);
        List<IFile> fileList4path = QueryUtils.findFileResources(filePath, QueryUtils.getSearchFolders());
        if (fileList4path.size() == 1) {
            QueryUtils.openFile(fileList4path.get(0));
            return;
        }

        // search file by name
        String fileName = QueryUtils.toFileName(m_nodeInfo);
        List<IFile> fileList4name = null;
        if (!fileName.equals(filePath)) {
            fileList4name = QueryUtils.findFileResources(fileName, QueryUtils.getSearchFolders());
            if (fileList4name.size() == 1) {
                QueryUtils.openFile(fileList4name.get(0));
                return;
            }
        }

        if (fileList4path != null && !fileList4path.isEmpty()) {
            QueryUtils.openFile(fileList4path.get(0));
            return;
        }

        if (fileList4name != null && !fileList4name.isEmpty()) {
            QueryUtils.openFile(fileList4name.get(0));
            return;
        }
    }
}
